<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业名称" prop="dictName">
        <el-input
          v-model="queryParams.data.name"
          placeholder="请输入企业名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="工手机号" prop="dictType">
        <el-input
          v-model="queryParams.dictType"
          placeholder="请输入员工手机号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="字典状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:dict:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:dict:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:dict:remove']"
        >删除</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:dict:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleRefreshCache"
          v-hasPermi="['system:dict:remove']"
        >刷新缓存</el-button>
      </el-col> -->
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange" border>
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="企业ID" align="center" prop="enterpriseInfoId" />
      <el-table-column label="负责人名称" align="center" prop="principalName" />
      <el-table-column label="负责人联系电话" align="center" :show-overflow-tooltip="true" prop="principalTel" />
      <el-table-column label="员工数" align="center" prop="useEmployeeNum" />
      <el-table-column label="详细地址" align="center" prop="detailAddress" />
      <el-table-column label="企业经营地址" align="center" prop="businessAddress" />
      <!-- <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="状态" align="center" prop="useStatus" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.useStatus == 1">使用中</span>
          <span v-if="scope.row.useStatus == 0">暂停</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dict:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row,1)"
            v-if="scope.row.useStatus == 0"
          >使用中</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row,0)"
            v-if="scope.row.useStatus == 1"
          >暂停</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />
<el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="企业名称" prop="name">
        <el-input v-model="form.name" placeholder="企业名称" />
      </el-form-item>
      <el-form-item label="负责人" prop="principalName">
        <el-input v-model="form.principalName" placeholder="负责人" />
      </el-form-item>
      <el-form-item label="联系电话" prop="principalTel">
        <el-input v-model="form.principalTel" placeholder="联系电话" />
      </el-form-item>
      <el-form-item label="员工数量" prop="useEmployeeNum">
        <el-input v-model="form.useEmployeeNum" placeholder="员工数量" />
      </el-form-item>
      <el-form-item label="企业地址" prop="name">
        <el-input v-model="form.detailAddress" placeholder="企业地址" />
      </el-form-item>
      <el-form-item label="企业经营地址" prop="name">
        <el-input v-model="form.businessAddress" placeholder="企业经营地址" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>

  </div>
</template>

<script>
import { listType2, getType, delType, addType, updateType, refreshCache,peopleupdateType } from "@/api/system/dict/type";
import { addpreson,deleteGosi,updataenterpris } from "@/api/addming";

export default {

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
          data: {
          name: ""
      },
        current: 1,
        size: 10,
        name:''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "企业名称不能为空", trigger: "blur" }
        ],
        principalName: [
          { required: true, message: "负责人不能为空", trigger: "blur" }
        ],
        principalTel: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        useEmployeeNum: [
          { required: true, message: "员工数量不能为空", trigger: "blur",  pattern: /^\d+$/, }
        ],
        detailAddress: [
          { required: true, message: "企业地址不能为空", trigger: "blur" }
        ],
        businessAddress: [
          { required: true, message: "企业经营地址不能为空", trigger: "blur" }
        ]
      }

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      listType2(this.queryParams).then(response => {
        debugger
          this.typeList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增企业";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dictId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dictId = row.enterpriseInfoId || this.ids
      peopleupdateType(dictId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业信息";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
          if (this.form.enterpriseInfoId) {
            addpreson(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addpreson(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
    },
    /** 更新 */
    handleDelete(item,type) {
      // 1.使用中 0.暂停
      let data = {
        enterpriseInfoId:item.enterpriseInfoId,
        useStatus:type
      }
      updataenterpris(data).then(() => {
        this.$modal.msgSuccess("更新成功");
        this.getList()
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/dict/type/export', {
        ...this.queryParams
      }, `type_${new Date().getTime()}.xlsx`)
    },
    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      refreshCache().then(() => {
        this.$modal.msgSuccess("刷新成功");
        this.$store.dispatch('dict/cleanDict');
      });
    }
  }
};
</script>
